/***
** ajax的请求包装
**/
define(function(){
    var httppre = cors_config.api_host;
    return (function(){
       this.post = function(url,body){
            return $.ajax({
              type: "POST",
              url: url,
              data: body,
              dataType:'json'
            });
        };

        this.get = function(url){
            return $.ajax({
              type: "GET",
              url: url,
              dataType:'json',
            });
        };

        /**
        *@url {String} 请求地址
        *@bd  {Object} 请求body 体
        */
        this.do = function(url,bd,type){
            if(typeof(bd) == "funciton") bd = new db();
            url = httppre + url;
            return $.ajax({
              type: type || (bd ?"POST":"GET"),
              url: url,
              data: bd,
              xhrFields: {
                    withCredentials: true
                }
            });
        };
        /*
         	与do同样的功能，用于简化调用
         	do参数写法：{param:JSON.stringify(batchIds)}
         	work参数写法:batchIds
        */
        this.work = function(url,bd,type){
            url = httppre + url;
            var _config = {
                url: url,
                xhrFields: {
                    withCredentials: true
                }
            }

            if (bd){
                _config.data =   {
                    param:JSON.stringify(bd)
                }
            }
            _config.type = type || (bd ?"POST":"GET");
            return $.ajax(_config);
        }
        /**
         *@url {String} 请求地址
         *@bd  {Object} 请求body 体
         */
        this.doAsync = function(url,bd,type){
            if(typeof(bd) == "funciton") bd = new db();
            url = httppre + url;
            return $.ajax({
                type: type || (bd ?"POST":"GET"),
                url: url,
                data: bd,
                async: false,
                xhrFields: {
                    withCredentials: true
                }
            });
        };

        /***
        **
        **opts  = {delay:100,cache:false,idkey:"id",textkey:"name"}
        **/
        this.s2AJAX = function(url,para,opts){
            return {
                url: url,
                delay:opts.delay||100,
                data: function (params) {// 请求参数的处理
                    para.q = params.term || "";
                    if (opts.lang && typeof(opts.lang) == "function") {
                        para.locale = opts.lang();
                    }
                    if (opts.tagName == "-1") para.tagName = params.term || "";

                    //额外传递传递参数，每次查询都会调用
                    if (opts.extParams && typeof(opts.extParams) == "function") {
                        var exts =  opts.extParams();
                        para =  $.extend({},para,exts);
                    }
                    var body = {param: JSON.stringify(para)};
                    return body;
                },
                processResults: function (result) {//返回结果的处理
                    if(typeof(result)=="string") result = JSON.parse(result);
                    var select2Data = $.map(result.data, function (obj) {
                        if(typeof(obj)=="string") obj = JSON.parse(obj);
                        obj.text = obj[opts.textkey||"name"];
                        if(opts.textFun && typeof(opts.textFun) == "function") obj.text = opts.textFun(obj);
                        if(opts.idkey) obj.id = obj[opts.idkey||"id"];
                        //if(opts.hasOwnProperty("isClude")) obj.isClude = opts.isClude;
                        if(opts.extProcessResults && typeof(opts.extProcessResults) == "function") obj = opts.extProcessResults(obj);
                        return obj;
                    });

                    if(opts.filterFun && typeof(opts.filterFun) == "function"){
                        select2Data = opts.filterFun(select2Data);
                    }

                  return {
                    results: select2Data
                  };
                },
                cache: true,
                transport: function (params, success, failure) {
                    var $request = self.do(params.url,params.data);
                    $request.then(success);
                    $request.fail(failure);
                    return $request;
                }
              }
        }
        return this;
    }());
});
